package io.xxx.omni.oss.custom

import com.google.gson.JsonObject
import io.xxx.omni.oss.common.format
import io.xxx.omni.oss.domain.Document
import io.xxx.omni.oss.domain.DocumentType
import java.time.LocalDateTime
import java.util.*

// 订单
// https://vop.vip.com/home#/api/method/detail/vipapis.marketplace.delivery.SovDeliveryService-1.0.0/getOrders
@Suppress("unused")
class WphTradeSynchronizer : WphSynchronizer() {

    override val documentType = DocumentType.TRADE

    override val method = "getOrders"

    override fun buildDocuments(context: Context, response: JsonObject): List<Document> {
        return response
            .getAsJsonObject("result")
            .getAsJsonArray("orders")
            .map {
                it as JsonObject
                val now = LocalDateTime.now()
                Document(
                    it["order_id"].asString,
                    it["status"].asString,
                    it.toString(),
                    now,
                    now,
                )
            }
    }

    override fun buildRequest(context: Context, pageNo: Int): Map<String, Any> {
        val (startTime, endTime) = context
        return TreeMap(
            mapOf(
                "request" to mapOf(
                    "query_start_time" to startTime.format(),
                    "query_end_time" to endTime.format(),
                    "page" to pageNo,
                    "limit" to pageSize,
                )
            )
        )
    }
}